﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;

public partial class CMSV2_Controls_FrontPagePost_FrontPagePostInfo : System.Web.UI.UserControl
{
    public int Id
    {
        get
        {
            if (!String.IsNullOrEmpty(Request["Id"]))
            {
                int value = 0;
                if (int.TryParse(Request["Id"], out value))
                {
                    return value;
                }
            }

            return -1;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.uiBtnDelete.Enabled = false;

            BindImages();

            if (this.Id > 0)
            {
                LoadPost();
            }
        }
    }

    private void BindImages()
    {
        using (IFrontPagePostImageService service = ServiceFactory.FrontPagePostImageService)
        {
            this.uiRepImages.DataSource = service.Find();
            this.uiRepImages.DataBind();
        }
    }

    private void LoadPost()
    {
        this.uiBtnDelete.Enabled = true;

        using (IFrontPagePostService service = ServiceFactory.FrontPagePostService)
        {
            FrontPagePost post = service.Find(this.Id);
            this.uiTbTitle.Text = post.Title;
            this.uiTbText.Text = post.Text;
            this.uiTbLink.Text = post.Link;
            switch (post.PostType.ToUpperInvariant())
            {
                case "NEWS":
                    this.uiPlhEditLink.Visible = false;
                    this.uiPlhShowLink.Visible = true;
                    this.uiLitType.Text = "Nyhed";
                    break;
                case "REVIEW":
                    this.uiPlhEditLink.Visible = false;
                    this.uiPlhShowLink.Visible = true;
                    this.uiLitType.Text = "Anmeldelse";
                    break;
                default:
                    this.uiPlhEditLink.Visible = true;
                    this.uiPlhShowLink.Visible = false;
                    this.uiTbLink.Text = post.Link;
                    this.uiLitType.Text = post.PostType;
                    break;
            }

            this.uiHfImageId.Value = "";
            if (post.ImageId.HasValue)
            {
                using (IFrontPagePostImageService imageService = ServiceFactory.FrontPagePostImageService)
                {
                    FrontPagePostImage image = imageService.Find(post.ImageId.Value);
                    if (image != null)
                    {
                        this.uiImgImage.Src = ResolveUrl("~/FrontBilled/" + image.FileName);
                        this.uiHfImageId.Value = image.Id.ToString();
                    }
                }
            }
        }
    }

    protected void uiRepImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.AlternatingItem:
            case ListItemType.Item:
                ImageButton button = e.Item.FindControl("uiBtnSelectImage") as ImageButton;
                FrontPagePostImage postImage = e.Item.DataItem as FrontPagePostImage;
                button.CommandArgument = postImage.Id.ToString();
                button.ImageUrl = ResolveUrl("~/FrontBilled/" + postImage.FileName);
                break;
        }
    }

    protected void uiBtnSelectImage_Click(object sender, EventArgs e)
    {
        ImageButton btn = sender as ImageButton;
        if (btn != null)
        {
            using (IFrontPagePostImageService imageService = ServiceFactory.FrontPagePostImageService)
            {
                FrontPagePostImage image = imageService.Find(int.Parse(btn.CommandArgument));
                if (image != null)
                {
                    this.uiImgImage.Src = ResolveUrl("~/FrontBilled/" + image.FileName);
                    this.uiHfImageId.Value = image.Id.ToString();
                }
            }
        }
    }

    protected void uiIuNewImage_ImageTransformationComplete(object sender, System.Drawing.Image newImage)
    {
        using (IFrontPagePostImageAdminService imageAdminService = ServiceFactory.FrontPagePostImageAdminService)
        {
            string filename = Guid.NewGuid().ToString() + ".jpg";
            newImage.Save(MapPath(ResolveUrl("~/FrontBilled/" + filename)), System.Drawing.Imaging.ImageFormat.Jpeg);

            FrontPagePostImage newPostImage = new FrontPagePostImage();
            newPostImage.FileName = filename;
            newPostImage.Tags = String.Empty;
            newPostImage = imageAdminService.Save(newPostImage);

            this.uiImgImage.Src = ResolveUrl("~/FrontBilled/" + newPostImage.FileName);
            this.uiHfImageId.Value = newPostImage.Id.ToString();

            BindImages();
        }
    }

    protected void uiBtnDelete_Click(object sender, EventArgs e)
    {
        using (IFrontPagePostAdminService service = ServiceFactory.FrontPagePostAdminService)
        {
            service.Delete(this.Id);
            Response.Redirect("Default.aspx");
        }
    }

    protected void uiBtnSave_Click(object sender, EventArgs e)
    {
        Page.Validate("FrontPagePost");
        if (Page.IsValid)
        {
            using (IFrontPagePostAdminService adminService = ServiceFactory.FrontPagePostAdminService)
            using (IFrontPagePostService service = ServiceFactory.FrontPagePostService)
            {
                FrontPagePost post = null;
                if (this.Id > 0)
                {
                    post = service.Find(this.Id);
                }
                else
                {
                    post = new FrontPagePost();
                    post.PostType = "Besked";
                }

                post.Title = this.uiTbTitle.Text;
                post.Text = this.uiTbText.Text;
                if (this.uiPlhEditLink.Visible)
                {
                    post.Link = this.uiTbText.Text;
                }

                int imageId = int.Parse(this.uiHfImageId.Value);
                if (imageId > 0)
                {
                    post.ImageId = imageId;
                }
                else
                {
                    post.ImageId = null;
                }

                post.IsActive = post.ImageId.HasValue;

                post = adminService.Save(post);

                if (this.Id < 0)
                {
                    Response.Redirect("FrontPagePostManager.aspx?Id=" + post.Id.ToString());
                }
            }
        }
    }
}
